﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ELGGAPI.DomainModels;
using ELGGAPI.Models;
using ELGGAPI.Data;
using System.Web.Http;

namespace ELGGAPI.Controllers
{
    public class ProviderController : ApiController
    {

        public Response<AutheticationToken> GetAuthenticate(string username, string password)
        {
            var response = new Response<AutheticationToken>();
            try
            {
                AutheticationToken AuthToken = AutheticationToken.Authenticate(username, password, 1);
                if (AuthToken == null)
                {
                    response.Data = null;
                    response.Message = "Authentication failed";
                }
                response.Data = AuthToken;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<Idclass> PostAddGameProvider(Request<ProviderModel> request)
        {
            var response = new Response<Idclass>();
            try
            {
                if (request.access_token == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null && !(request.access_token.Equals(APIHelper.SecretToken)))
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }

                Idclass Id = ProviderDomainModles.AddProdiver(request.Data);

                if (Id.id == -100)
                {
                    response.Data = null;
                    response.Message = "User name duplicated.";
                    return response;
                }

                response.Data = Id;
                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }

    }
}
